package com.communication.controller;

import java.util.Arrays;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import com.communication.json.CommunicationEntity;
import com.communication.json.JsonResult;
import com.entity.Measuringpoint;
import com.file.service.MeasuringpointService;
import com.file.service.MeterService;
import com.file.service.TerminalService;
import com.protocol.sentjson.SocketData;
import com.protocolmeter.ProtocolMerterService;
import com.protocolterminal.ProtocolTerminalService;
import com.protocolterminal.base.Seq;

import io.swagger.annotations.ApiOperation;

@Controller
@RequestMapping(value = "/control")
public class Control {

	@Value("${socket.port}")
	private String socketport;

	@Autowired
	private TerminalService terminalservice;
	@Autowired
	private MeasuringpointService measuringpointService;
	@Autowired
	private MeterService meterService;

	@Value("${meter.keys}")
	private String keys;

	@ApiOperation(value = "拉合闸控制", notes = "")
	@RequestMapping(value = "/sent", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult<CommunicationEntity> control(@RequestBody JsonResult<CommunicationEntity> jsonResult,
			HttpServletRequest request) {

		String key = keys;
		int type = jsonResult.getState();
		String meterid = jsonResult.getMessage();
		String data = "";
		String control = "1C";
		String identification = "00000000";
		switch (type) {
		case 1:// 立刻拉闸
			data = "1A00-990101000000";
			break;
		case 2:// 合闸允许
			data = "1B00-990101000000";
			break;
		case 3:// 立刻合闸
			data = "1C00-990101000000";
			break;
		case 4:// 保电
			data = "3A00-990101000000";
			break;
		case 5:// 保电解除
			data = "3B00-990101000000";
			break;
		case 6:// 密钥下装
			data = keys;
			key = "0101010101010101";
			control = "03";
			identification = "070301FF";
			break;
		case 7:// 密钥恢复
			key = keys;
			data = "0101010101010101";
			control = "03";
			identification = "070301FF";
			break;
		default:
			break;
		}
		// 获取信息
		String meteraddress = meterService.findMeteraddressByMeterid(Integer.valueOf(meterid));
		String meterprotocol = meterService.findMeterprotocolByMeterid(Integer.valueOf(meterid));
		int terminalid = measuringpointService.findTerminalidByMeterid(Integer.valueOf(meterid));
		String terminalprotocol = terminalservice.findTerminalprotocolByTerminalid(terminalid);
		String terminaladdress = terminalservice.findTerminaladdressByTerminalid(terminalid);
		Measuringpoint measuringpoint = measuringpointService.findByTerminalidAndMeterid(terminalid,
				Integer.valueOf(meterid));

		ProtocolMerterService protocolMerterService = new ProtocolMerterService();
		String frame645 = protocolMerterService.Encode(Integer.valueOf(meterprotocol), meteraddress, control,
				identification, "00000000", data, "00000000", key);

		String datas = measuringpoint.getMeasuringpointport() + "-" + measuringpoint.getMeasuringpointbaud()
				+ "-0-1-0-8-200-0-100-" + frame645;

		ProtocolTerminalService protocolTerminalService = new ProtocolTerminalService();
		String sentframe = protocolTerminalService.Encode(Integer.valueOf(terminalprotocol), "10",
				new Seq(0, 1, 1, 1, 0).toString(), Arrays.asList("0000".split("-")), Arrays.asList("0100".split("-")),
				Arrays.asList(datas.split(";")), "", 1, 11, terminaladdress);

		// 发送
		if (sentframe != null && !sentframe.equals("")) {
			// http请求
			String urlsent = "http://" + socketport + "/SocketControl/sent/realtime";
			RestTemplate restTemplatesent = new RestTemplate();
			// 组发送内容
			JsonResult<SocketData> sentjson = new JsonResult<>();
			SocketData socketData = new SocketData();
			socketData.setKey(request.getSession().getId());
			socketData.setMessage(sentframe);
			socketData.setTerminaladdress(terminaladdress);
			sentjson.setData(socketData);

			@SuppressWarnings("rawtypes")
			ResponseEntity<JsonResult> returnjson = restTemplatesent.postForEntity(urlsent, sentjson, JsonResult.class);
			// 终端不在线，终端通讯中

			jsonResult.setState(returnjson.getBody().getState());
			@SuppressWarnings("unchecked")
			Map<String, String> map = (Map<String, String>) returnjson.getBody().getData();
			request.getSession().setAttribute("randon", map.get("randonint"));
		}

		return jsonResult;
	}

}
